import {actionTypes} from "./actionTypes"
import { adminLogin} from "../api/admin";
import {message} from "antd";

export const getAdmin = (dispatch,data) => {
    return (async (dispatch,data) => {
        let info = null;
        await adminLogin(data).then(res => {
            message.success('登录成功')
            info = res.data;
            dispatch({
                type: actionTypes.GetAdmin,
                data: info
            })
            if (data.remember){
                window.localStorage.setItem('isManagerLogin',JSON.stringify(true));
                window.localStorage.setItem("ManagerInfo",JSON.stringify(info));
            }else{
                window.sessionStorage.setItem('isManagerLogin',JSON.stringify(true));
                window.sessionStorage.setItem('ManagerInfo',JSON.stringify(info));
            }
        }).catch(err => {
            dispatch({
                type: actionTypes.exitAdmin
            })
            console.log(err)
        })
    })(dispatch,data)
}

export const exit = () => {
    return {
        type: actionTypes.exitAdmin
    }
}

export const loading = () => {
    return {
        type: actionTypes.LoadingAdmin
    }
}
